package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alicp.jetcache.anno.Cached;
import com.yungu.swift.assets.driver.model.dto.CarModelDto;
import com.yungu.swift.assets.driver.service.CarModelService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.dao.SysBusinessMapper;
import com.yungu.swift.system.sys.model.dto.SysBusinessDto;
import com.yungu.swift.system.sys.service.SysBusinessService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * SystemBusinessServiceImpl
 **/
@Service
public class SysBusinessServiceImpl extends BaseServiceImpl<SysBusinessDto> implements SysBusinessService {
    @Autowired
    private SysBusinessMapper systemBusinessMapper;
    @Reference
    private CarModelService carModelService;

    @Override
    protected IMapper<SysBusinessDto> getMapper() {
        return systemBusinessMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<Boolean> init(String appid) {
        //判断是否已经初始化过
        List<SysBusinessDto> sysBusinessDtos = list(MapUtils.build()).getData();
        if (CollectionUtils.isNotEmpty(sysBusinessDtos)) {
            return ResponseData.buildSuccessResponse("已经执行过，无法再次执行", Boolean.TRUE);
        }
        try {
            //出租车业务
            SysBusinessDto sysBusinessTaxiDto = new SysBusinessDto();
            sysBusinessTaxiDto.setUuid(StringUtils.buildUUID());
            sysBusinessTaxiDto.setName("出租车");
            sysBusinessTaxiDto.setType(CommonConstant.BUSINESS_TYPE_TAXI);
            sysBusinessTaxiDto.setStatus(CommonConstant.STATUS_OPENED);
            add(sysBusinessTaxiDto);

            //专车业务
            SysBusinessDto sysBusinessSpecDto = new SysBusinessDto();
            sysBusinessSpecDto.setUuid(StringUtils.buildUUID());
            sysBusinessSpecDto.setName("专车");
            sysBusinessSpecDto.setType(CommonConstant.BUSINESS_TYPE_SPEC);
            sysBusinessSpecDto.setStatus(CommonConstant.STATUS_OPENED);
            add(sysBusinessSpecDto);

            //拼车业务
            SysBusinessDto sysBusinessPoolDto = new SysBusinessDto();
            sysBusinessPoolDto.setUuid(StringUtils.buildUUID());
            sysBusinessPoolDto.setName("跨城拼车");
            sysBusinessPoolDto.setType(CommonConstant.BUSINESS_TYPE_POOL);
            sysBusinessPoolDto.setStatus(CommonConstant.STATUS_OPENED);
            add(sysBusinessPoolDto);

            //快车业务
            SysBusinessDto sysBusinessFastDto = new SysBusinessDto();
            sysBusinessFastDto.setUuid(StringUtils.buildUUID());
            sysBusinessFastDto.setName("快车");
            sysBusinessFastDto.setType(CommonConstant.BUSINESS_TYPE_EXPRESS);
            sysBusinessFastDto.setStatus(CommonConstant.STATUS_OPENED);
            add(sysBusinessFastDto);

            //出租车车型
            CarModelDto carModelTaxiDto = new CarModelDto();
            carModelTaxiDto.setUuid(StringUtils.buildUUID());
            carModelTaxiDto.setModelName("出租车");
            carModelTaxiDto.setModelType(1);
            carModelTaxiDto.setAppid(appid);
            carModelTaxiDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelTaxiDto);

            //专车车型：舒适型
            CarModelDto carModelSpecComfortableDto = new CarModelDto();
            carModelSpecComfortableDto.setUuid(StringUtils.buildUUID());
            carModelSpecComfortableDto.setModelName("舒适型");
            carModelSpecComfortableDto.setModelType(1);
            carModelSpecComfortableDto.setAppid(appid);
            carModelSpecComfortableDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelSpecComfortableDto);

            //专车车型：商务型
            CarModelDto carModelSpecBusinessDto = new CarModelDto();
            carModelSpecBusinessDto.setUuid(StringUtils.buildUUID());
            carModelSpecBusinessDto.setModelName("商务型");
            carModelSpecBusinessDto.setModelType(2);
            carModelSpecBusinessDto.setAppid(appid);
            carModelSpecBusinessDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelSpecBusinessDto);

            //专车车型：豪华型
            CarModelDto carModelSpecDeluxeDto = new CarModelDto();
            carModelSpecDeluxeDto.setUuid(StringUtils.buildUUID());
            carModelSpecDeluxeDto.setModelName("豪华型");
            carModelSpecDeluxeDto.setModelType(3);
            carModelSpecDeluxeDto.setAppid(appid);
            carModelSpecDeluxeDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelSpecDeluxeDto);

            //跨城拼车
            CarModelDto carModelPoolDto = new CarModelDto();
            carModelPoolDto.setUuid(StringUtils.buildUUID());
            carModelPoolDto.setModelName("跨城拼车");
            carModelPoolDto.setModelType(3);
            carModelPoolDto.setAppid(appid);
            carModelPoolDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelPoolDto);

            //快车型
            CarModelDto carModelFastDto = new CarModelDto();
            carModelFastDto.setUuid(StringUtils.buildUUID());
            carModelFastDto.setModelName("快车");
            carModelFastDto.setModelType(1);
            carModelFastDto.setAppid(appid);
            carModelFastDto.setStatus(CommonConstant.STATUS_OPENED);
            carModelService.add(carModelFastDto);
            return ResponseData.buildSuccessResponse("初始化成功", Boolean.TRUE);
        } catch (Exception e) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "初始化失败", Boolean.FALSE);
        }
    }

    @Cached(name = "jetCache:system:getBusinessUuidByType:",key = "#type",expire = 100)
    @Override
    public ResponseData<String> getBusinessUuidByType(Integer type) {
        return ResponseData.buildSuccessResponse(systemBusinessMapper.getBusinessUuidByType(type));
    }
}